﻿using System;
using System.Text;
using System.Threading;
using OtherDevice;
using System.ComponentModel;
using System.Windows.Forms;
namespace OtherDevice
{
    [Description("程控电源- GPD2303")]
    public class GPD2303S : ProgramPowerBase
    {
        public GPD2303S(string strName) : base(strName)
        {
            controlFrom = new Form_程控电源();
        }


        public override bool GetI(int ch, ref double val)
        {
            try
            {
                string info = $"IOUT{ch + 1}?\r\n";
                byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(info);
                byte[] byteRe = new byte[] { };
                if (SendAndReciveData(byteArray, ref byteRe, 500))
                {
                    string str = Encoding.ASCII.GetString(byteRe);
                    string[] strs = str.Split('A');
                    val = double.Parse(strs[0]);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                val = 0;
                return false;
            }

        }
        public override bool Power(bool b)
        {
            //if (bOpen)
            //    return;
            //bOpen = true;
            int val = b ? 1 : 0;
            string info = $"OUT{val}\r\n";
            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(info);
            byte[] byteRe = new byte[] { };
            return SendAndReciveData(byteArray, ref byteRe, 500);

        }
        public override bool GetV(int ch, ref double val)
        {
            try
            {
                string info = $"VOUT{ch + 1}?\r\n";
                byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(info);
                byte[] byteRe = new byte[] { };
                if (SendAndReciveData(byteArray, ref byteRe, 500))
                {
                    string str = Encoding.ASCII.GetString(byteRe);
                    string[] strs = str.Split('A');
                    val = double.Parse(strs[0]);
                    return true;
                }
                else
                {
                    return false;
                }           
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                val = 0;
                return false;
            }

        }
        public override bool SetV(int ch, double val)
        {
            string info = $"VSET{ch + 1}:{val.ToString("F1")}\n";
            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(info);
            byte[] byteRe = new byte[] { };
            return SendAndReciveData(byteArray, ref byteRe, 500);
        }

        public override bool SetI(int ch, double val)
        {
            string info = $"ISET{ch + 1}:{val.ToString("F1")}\n";
            byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(info);
            int n_RetryTime = 0;
        Retry:
            SendData(byteArray);
            byte[] byteRe = new byte[] { };
            int ret = ReceiveData(ref byteRe, 500);
            if (ret != 0)
            {
                n_RetryTime++;
                if (n_RetryTime < 3)
                    goto Retry;
                else
                    return false;
            }
            else
            {
                return true;
            }
        }
    }
}